<template>
  <div class="backColor">
    <div class="swiper">
      <van-swipe :autoplay="3000" indicator-color="#C30101">
        <van-swipe-item
          v-for="(item, index) in info.data.listAlbum"
          :key="index"
          @click="seeImage(index)"
        >
          <img :src="item" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="content">
      <div class="title">
        {{ info.data.name }}
      </div>
      <div class="number">商品编号：{{ info.data.number }}</div>
      <div class="money">
        <span>价格</span>
        <div>
          ￥<span>{{ info.data.price }}</span>
        </div>
      </div>
      <div class="quantity">
        购买数量 &nbsp;&nbsp;&nbsp;<span>{{
          info.data.productStatus == "0" ? `库存数量：1` : `已售空`
        }}</span>
      </div>
    </div>
    <div class="details">
      <div class="text">商品详情</div>
      <div class="html" v-html="info.data.detail"></div>
    </div>
    <div class="buttons">
      <div class="left" @click="router.go(-1)">返回列表</div>
      <div class="right" @click="submit" v-if="info.data.productStatus == '0'">
        立即购买
      </div>
      <div class="right" style="background-color: #e5e5e5; color: #000" v-else>
        已售罄
      </div>
    </div>
  </div>
  <van-popup
    v-model:show="show"
    @click-overlay="clickOverlay"
    style="width: 80%; background-color: var(--backColor)"
  >
    <Popup
      :money="info.data.price"
      :productId="route.query.id"
      :myId="myId"
      :Id="Id"
      @onRmove="onRmove"
    />
  </van-popup>
</template>

<script>
import {
  defineComponent,
  onMounted,
  provide,
  reactive,
  ref,
  getCurrentInstance,
} from "vue";
import { useRouter, useRoute } from "vue-router";
import { ImagePreview } from "vant";
import Popup from "@/components/Popup.vue";
import {
  getActiveDetail,
  imgUrl,
  setProductBuy,
  getAddressList,
} from "@/api/http.js";

export default defineComponent({
  setup() {
    const router = useRouter();
    const route = useRoute();
    const { proxy } = getCurrentInstance();
    const info = reactive({
      data: {},
    });
    const seeImage = (index) => {
      ImagePreview({
        images: info.data.listAlbum,
        startPosition: index,
      });
    };

    const show = ref(false);
    const myId = ref("");
    const Id = ref("");
    const submit = async () => {
      const data = await setProductBuy({ id: route.query.id });
      if (data.code != 200) return proxy.$toast.fail(data.msg);
      myId.value = data.data;
      Id.value = info.data.id;
      show.value = true;
      overlayBool.value = false;
    };

    const subBool = async () => {
      const { data } = await getAddressList();
      if (data.length == 0) {
        proxy.$toast.fail("请设置收货地址");
        setTimeout(() => {
          router.push({
            path: "/address",
          });
        }, 2000);

        return;
      }
    };

    const onRmove = () => {
      console.log("取消");
      show.value = false;
      getInfo();
    };

    const overlayBool = ref(false);
    provide("overlay", overlayBool);
    const clickOverlay = () => {
      overlayBool.value = true;
    };

    const getInfo = async () => {
      const data = await getActiveDetail({ id: route.query.id });
      data.data.listAlbum = data.data.listAlbum
        .split(",")
        .map((item) => imgUrl + item);
      if (data.code != 200) return proxy.$toast.fail("获取商品信息失败");
      info.data = data.data;
      console.log("获取商品详情---", data);
    };

    onMounted(() => {
      subBool();
      getInfo();
    });

    return {
      seeImage,
      info,
      router,
      route,
      submit,
      show,
      onRmove,
      clickOverlay,
      overlayBool,
      myId,
      Id,
    };
  },
  components: {
    Popup,
  },
});
</script>

<style lang="less" scoped>
.backColor {
  min-height: 100vh;
  background-color: var(--backColor);
  .swiper {
    width: 100%;
    height: 300px;

    img {
      // width: 100%;
      height: 300px;
    }
  }
  .content {
    width: 100%;
    padding: 20px 4%;
    background-color: var(--backColor);
    color: var(--textColor);

    .title {
      font-size: 1.125rem;
      line-height: 25px;
      letter-spacing: 0.5px;
      font-family: PingFang SC;
      font-weight: bold;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2; /*想省略几行就写几*/
      -webkit-box-orient: vertical;
    }

    .number {
      margin-top: 20px;
      font-size: 0.75rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: var(--textColorTwo);
    }

    .money {
      margin-top: 20px;
      font-size: 0.75rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: var(--textColorTwo);
      display: flex;
      justify-content: space-between;
      align-content: flex-end;

      & > span {
        line-height: 35px;
      }

      div {
        color: #dd0000;

        span {
          font-size: 1.56rem;
        }
      }
    }

    .quantity {
      margin-top: 10px;
      font-size: 0.75rem;
      font-family: PingFang SC;
      font-weight: 400;
      padding-bottom: 20px;
      border-bottom: 1px solid var(--textColorTwo);

      span {
        font-size: 0.7rem;
        color: var(--textColorTwo);
      }
    }
  }
  .details {
    width: 100%;
    padding: 20px 4% 60px;
    background-color: var(--backColor);
    color: var(--textColor);

    .html {
      width: 100%;
      margin-top: 20px;
      overflow: hidden;
    }
  }
  .buttons {
    width: 100%;
    height: 60px;
    position: fixed;
    left: 0;
    z-index: 99;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    letter-spacing: 2px;

    div {
      width: 50%;
      height: 100%;
      font-size: 1.125rem;
      font-family: PingFang SC;
      font-weight: bold;
      line-height: 60px;
      text-align: center;
    }

    .left {
      background-color: #fff;
      color: #dd0000;
    }
    .right {
      background-color: #dd0000;
      color: #fff;
    }
  }
}
.dialog {
  img {
    width: 100%;
  }
}
</style>
